// BASE SETUP
import express from 'express';
let app = express();
let bodyParser = require('body-parser');
var router = require('./api/api.js');


//configure app to use bodyParser()
//this will let us get the data from a POST

app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());


let port = process.env.PORT || 8000;

//ROUTES FOR OUR API

// var router = express.Router();

// test route to make sure everything is working (accessed at GET http://localhost:8000/api)

/*router.get('/',function(req, res){
	res.json({ message: 'welcome to our api!'});
});*/


// more routes for our API will happen here

// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api

app.get('/', (req, res) => {
    res.send(`YO! YO! YO! HA~~`);
});

// 所有的请求都走api/
app.use('/api', router);

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// START THE SERVER
// =============================================================================
app.listen(port, () => {
    console.log(`server running http://localhost:${port}`);
});

